package datastructure.segmenttree;

/**
 * @author XY
 * @version 1.0.0
 * @ClassName SegmentTree.java
 * @createTime 2021年12月14日 19:23:00
 * 线段树
 */
public class SegmentTree<E> {

    private E[] data;

    //要转换成线段树的数组，大小为四倍的data
    private E[] tree;

    private Merger<E> merger;

    public SegmentTree(E[] arr,Merger<E> merger){
        this.merger= merger;
        data = (E[])new Object[arr.length];
        for (int i = 0; i < arr.length; i++) {
            data[i] = arr[i];
        }
        tree = (E[])new Object[4*data.length];
        buildSegmentTree(0,0,data.length-1);
    }

    //在treeIndex的位置创建表示区间[l..r]的线段树
    private void buildSegmentTree(int treeIndex,int l,int r){
        //如果区间长度为0，即叶子节点，那节点值即为数组元素值
        if(l==r){
            tree[treeIndex] = data[l];
            return ;
        }
        int leftTreeIndex = leftChild(treeIndex);
        int rightTreeIndex = rightChild(treeIndex);
        int mid = l+(r-l)/2;
        buildSegmentTree(leftTreeIndex,l,mid);
        buildSegmentTree(rightTreeIndex,mid+1,r);
        //定义的线段树的节点代表的是什么含义，这里是区间内的和
        tree[treeIndex] = merger.merge(tree[leftTreeIndex],tree[rightTreeIndex]);
    }

    //返回区间 [queryL,queryR]的值
    public E query(int queryL,int queryR){
        return query(0,0,data.length-1,queryL,queryR);
    }

    //在以treeId 为根的线段树中[l..r]的范围里  搜索[queryL,queryR]的值
    private E query(int treeIndex, int l,int r,int queryL,int queryR){
        if(l==queryL && r ==queryR){
            return tree[treeIndex];
        }
        int mid = l + (r-l)/2;
        int leftTreeIndex = leftChild(treeIndex);
        int rightTreeIndex = rightChild(treeIndex);
        //如果要查的区间在右孩子
        if(queryL>=mid+1){
            return query(rightTreeIndex,mid+1,r,queryL,queryR);
        //如果要查的区间在左孩子
        }else if(queryR <= mid){
            return query(leftTreeIndex,l,mid,queryL,queryR);
        }
        //如果要查的区间由左右孩子合并
        E leftResult = query(leftTreeIndex, l, mid, queryL, mid);
        E rightResult = query(rightTreeIndex, mid + 1, r, mid + 1, queryR);
        return merger.merge(leftResult,rightResult);
    }

    public void set(int index,E val){
        if(index<0||index>=data.length){
            throw new RuntimeException("index is illegal");
        }
        data[index] = val;
        set(0,0,data.length-1,index,val);
    }

    //线段树更新操作
    private void set(int treeIndex, int l, int r,int index,E e){
        if(l==r){
            tree[treeIndex] = e;
            return;
        }
        int mid = l + (r-l)/2;
        int leftTreeIndex = leftChild(treeIndex);
        int rightTreeIndex = rightChild(treeIndex);
        if(index>=mid+1){
            set(rightTreeIndex,mid+1,r,index,e);
            //如果要查的区间在左孩子
        }else if(index <= mid){
            set(leftTreeIndex,l,mid,index,e);
        }
        tree[treeIndex] = merger.merge(tree[leftTreeIndex],tree[rightTreeIndex]);

    }

    public int getSize(){
        return data.length;
    }

    public E get(int index){
        if(index<0||index>data.length){
            throw new RuntimeException("index is illegal;");

        }
        return data[index];
    }

    private int leftChild(int index){
        return 2*index+1;
    }

    private int rightChild(int index){
        return  2*index+2;
    }

}
